package com.example.wxchatdemo.utils;

import com.example.wxchatdemo.pojo.WeChat_msgs;
import com.example.wxchatdemo.pojo.Wechatuser;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MyFriends extends Thread implements Callable {
    private String address="100.2.193.182";
    private String value;
    private String my_id;
    private List<Wechatuser> lists;
    private  int x;

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public List<Wechatuser> getLists() {
        return lists;
    }

    public void setLists(List<Wechatuser> lists) {
        this.lists = lists;
    }



    public void setMy_id(String my_id) {
        this.my_id = my_id;
    }

    public void run() {
        String result = null;
        OkHttpClient client = new OkHttpClient();

        Request request = new Request.Builder()
                .url("http://"+address+":8085/GetUsers?myid="+my_id).build();
        //数据是：//phone="+phone.getText()+"&name="+username.getText()+"&password="+password.getText())

        try (Response response = client.newCall(request).execute()) {

            result = response.body().string();
            //转化JSON函数
            TransJSONtoUsers transJSON=new TransJSONtoUsers();
            List<Map<String, String>> list = transJSON.ttransJSON(result);

            System.out.println("获得结果friends数量"+list.size());
            //循环取出json
            //  while (inde<list.size()){
            for (int i=0;i<list.size();i++){
                String id=list.get(i).get("id");
                String aname= list.get(i).get("aname");
                String aspeak= list.get(i).get("aspeak");
                String anumber= list.get(i).get("anumber");
                String lasttime= list.get(i).get("lasttime");
                String myid= list.get(i).get("myid");
                String friends= list.get(i).get("friends");


               // System.out.println(id+"==="+aname+"==="+aspeak+"==="+anumber+"=="+lasttime+"==="+myid+"===="+friends);

                //System.out.println("GetJSon方法结果集"+"id="+id+"---myid--="+myid+"--youid="+youid+"--time="+times+"==="+content);


                //创建实体类
                Wechatuser wechatuser = new Wechatuser(id, aname, aspeak, anumber, lasttime, myid, friends);
               List<Wechatuser> alist=new ArrayList<Wechatuser>();
               alist.add(wechatuser);
               setLists(alist); // System.out.println("在Myhendl中出现了status的数量+"+status);
                setX(1);

            }
            // System.out.println("查询的数据是==="+listss.size());





        } catch (IOException e) {
          //  List<Wechatuser> alist=new ArrayList<Wechatuser>();
            setLists(null);
            System.out.println("======MyFriends是空的");
            setX(1);
            //e.printStackTrace();

        }

    }//run



    public List<Wechatuser> call() throws Exception {
        while (true)
        {
            if (getX()==1){
                return lists;
            }

        }

    }
}
